using System;
using System.Linq;
using Taimila.Entify;
using Taimila.Entify.Attributes;

namespace Taimila.Entify.UnitTests
{
	/// <summary>
	/// Test entity type that has one single relationship
	/// property to itself. In real world this could be for
	/// example Person that has property Mom type of Person.
	/// </summary>
	[Entity(Version = 1.0)]
	public class EntityWithSingleSelfRelation : Entity
	{
		[EntityProperty(PersistentName = "TextProperty")]
		public string Text
		{
			get;
			set;
		}
		
		private EntityWithSingleSelfRelation subEntity;
		private long? subEntityLazyLoadIdentifier;
		
		[EntityProperty(PersistentName = "Relationship", Relationship = true)]
		public EntityWithSingleSelfRelation SubEntity
		{
			get
			{
				if(this.subEntityLazyLoadIdentifier == null && this.subEntity == null)
				{
					return null;	
				}
				else if(this.subEntity == null)
				{
					this.subEntity = this.LazyLoadEntity<EntityWithSingleSelfRelation>(this.subEntityLazyLoadIdentifier);
					return this.subEntity;
				}
				else
				{
					return this.subEntity;	
				}
			}
			set
			{
				if(value == null)
				{
					this.subEntityLazyLoadIdentifier = null;
					this.subEntity = null;
				}
				else
				{
					this.subEntityLazyLoadIdentifier = value.GetEntityId();
					this.subEntity = value;
				}
			}
		}
	}
}
